package com.woniuxy.micropermissionserver.adminmanage.controller;

import com.woniuxy.microcommonserver.bean.permission_hcz.Admin;
import com.woniuxy.microcommonserver.bean.permission_hcz.Permission;
import com.woniuxy.microcommonserver.res.ResultMsg;
import com.woniuxy.microcommonserver.util.JWTUtil;
import com.woniuxy.micropermissionserver.permissionmanage.service.IPermissionService;
import com.woniuxy.micropermissionserver.redis.RedisCache;
import com.woniuxy.micropermissionserver.rolemanage.service.IRoleService;
import com.woniuxy.micropermissionserver.util.TokenUtil;
import com.woniuxy.micropermissionserver.vo.RouterVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sys")
public class LoginController {


    @Resource
    private IRoleService roleService;

    @Resource
    private IPermissionService permissionService;

    @Resource
    private RedisCache redisCache;

    @PostMapping("login")
    public ResultMsg login(@RequestBody Map<String, Object> map) {
        Subject subject = SecurityUtils.getSubject();
        ResultMsg resultMsg = new ResultMsg(200, "登录成功", null);

        if (!subject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(map.get("username").toString(), map.get("password").toString());
            try {
                subject.login(token);

            } catch (Exception e) {
                resultMsg.setCode(10000);
                resultMsg.setMsg("用户名或密码错误");
            }

        }

        Map<String,Object> claims = new HashMap<>();
        claims.put("loginName",map.get("username"));
        resultMsg.setData(JWTUtil.createToken(claims));
        return resultMsg;
    }

    @GetMapping("logout")
    public ResultMsg logout() {
        Subject subject = SecurityUtils.getSubject();
        redisCache.deleteObject(TokenUtil.resolveToken());
        subject.logout();
        return new ResultMsg(200, "退出成功", null);
    }

    @GetMapping("getInfo")
    public ResultMsg<Map<String, Object>> getInfo() {
        Map<String, Object> map = new HashMap<>();
        Admin admin = redisCache.getCacheObject(TokenUtil.resolveToken());
        List<String> roles = roleService.findByAdmin(admin);
        map.put("roles", roles);
        List<String> perms = permissionService.findPermsByAdmin(admin);
        map.put("permissions", perms);
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("userName", "admin");
        map.put("user", userMap);
        return ResultMsg.<Map<String, Object>>builder().msg("success").code(200).data(map).build();
    }

    @GetMapping("getRouters")
    public ResultMsg<List<RouterVo>> getRouters() {
        Admin admin = redisCache.getCacheObject(TokenUtil.resolveToken());
        List<Permission> permissions = permissionService.findPermissionTreeByAdminId(admin.getId());
        return ResultMsg.<List<RouterVo>>builder().code(200).msg("success").data(permissionService.buildMenus(permissions)).build();
    }
}
